package com.su83.printerdroid;

/**
 * @author robert
 * 
 */
public class Printcore {

	private StringBuffer mainqueue;
	private StringBuffer priqueue;
	private StringBuffer sentlines; // Lines already Sent to printer
	private int lineno;
	private int queueindex;
	private int resendfrom;
	private boolean connected;
	private boolean online;

	public Printcore() {
		mainqueue = new StringBuffer();
		priqueue = new StringBuffer();
		lineno = -1;
		queueindex = -1;
		resendfrom = -1;
		connected = false;
		online = false;
	}

	public void connect() {
		
		if (connected) {
			disconnect();			
		}		
		//if usb accessory connected and ready
		enableListen();	
		
	}

	public void disconnect() {

	}

	public void pause() {

	}

	public void resume() {

	}

	public void reset() {

	}

	public void readline() { // ????

	}

	/**
	 * Enable the reading and decoding of messages from the printer
	 */
	public void enableListen() {
		if (!connected) {// wait for accessory to connect}

			if (connected) {
				while (!online) {
					// send m105 every 5 ticks
					
				}
			}
		}
		// go to the main activity and send a M105
	}

	/**
	 * @param cmd
	 *            a N + Line Number
	 * @return checksum of a command according to http://reprap.org/wiki/G-code
	 */
	public int checksum(char[] cmd) {
		int cs = 0;
		for (int i = 0; (cmd[i] != '*') && (Character.isDefined(cmd[i])); i++)
			cs = cs ^ cmd[i];
		cs &= 0xff;
		return cs;
	}
}
